package controller

import (
	"smc-admin/models"
	"smc-admin/response"
	"smc-admin/utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Quality struct {
	FileList         string `form:"fileList"`                            //文件
	Name             string `form:"name" binding:"required"`             //客户姓名
	Phone            string `form:"phone" binding:"required"`            //联系电话
	CarType          string `form:"carType" binding:"required"`          //车辆型号
	CarNum           string `form:"carNum" binding:"required"`           //车牌号
	CarID            string `form:"carID" binding:"required"`            //车架号
	Company          string `form:"company" binding:"required"`          //施工单位
	ConstructionTime string `form:"constructionTime" binding:"required"` //施工时间
	ConstructionPart string `form:"constructionPart" binding:"required"` //施工部位
	ScrollNum        string `form:"scrollNum" binding:"required"`        //卷轴号码
	QualityTime      string `form:"qualityTime" binding:"required"`      //质保时间
	Price            string `form:"price" binding:"required"`            //价格
	TypesID          int    `form:"typesId" binding:"required"`          //类型
}

type QualityById struct {
	ID int `form:"id" binding:"required"`
	Quality
}

// @Tags         质保管理
// @Summary      质保更新
// @Param        quality  body	Quality  true  "质保"
// @Success      200       {object}  response.Body
// @Router       /quality/update [PUT]
func UpdateQuality(ctx *gin.Context) {
	var quality QualityById
	err := ctx.ShouldBindJSON(&quality)
	if err != nil {
		panic(err.Error())
	}

	//更新
	i := models.Quality{
		BaseModel: models.BaseModel{
			ID: quality.ID,
		},
	}
	updateErr := i.Update(map[string]interface{}{
		"name":              quality.Name,
		"file_list":         quality.FileList,
		"phone":             quality.Phone,
		"car_type":          quality.CarType,
		"car_num":           quality.CarNum,
		"car_id":            quality.CarID,
		"company":           quality.Company,
		"price":             quality.Price,
		"scroll_num":        quality.ScrollNum,
		"quality_time":      quality.QualityTime,
		"construction_time": quality.ConstructionTime,
		"construction_part": quality.ConstructionPart,
		"types_id":          quality.TypesID,
	})
	if updateErr != nil {
		panic(updateErr.Error())
	}
	response.Success(ctx)
}

// @Tags         质保管理
// @Summary      质保添加
// @Param        quality  body	Quality  true  "质保"
// @Success      200       {object}  response.Body
// @Router       /quality/add [POST]
func AddQuality(ctx *gin.Context) {
	var quality Quality
	error := ctx.ShouldBindJSON(&quality)
	if error != nil {
		panic(error.Error())
	}

	i := models.Quality{
		TypesID:          quality.TypesID,
		FileList:         quality.FileList,
		Name:             quality.Name,
		Phone:            quality.Phone,
		CarType:          quality.CarType,
		CarNum:           quality.CarNum,
		CarID:            quality.CarID,
		Company:          quality.Company,
		Price:            quality.Price,
		ScrollNum:        quality.ScrollNum,
		QualityTime:      quality.QualityTime,
		ConstructionTime: quality.ConstructionTime,
		ConstructionPart: quality.ConstructionPart,
	}
	e := i.Add()
	if e != nil {
		panic(e.Error())
	}

	response.Success(ctx)
}

// @Tags         质保管理
// @Summary      质保分页查询
// @Param        id	query	int  false  "id"
// @Param        phone	query	string  false  "电话"
// @Param        typesId	query	int  false  "类型id"
// @Param        pageSize	query	string  false  "分页大小"
// @Param        pageNum	query	string  false  "分页数量"
// @Success      200       {object}  response.Body
// @Router       /quality/findByPage [get]
func FindQualityByPage(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Query("id"))
	typesId, _ := strconv.Atoi(ctx.Query("typesId"))

	i := models.Quality{
		BaseModel: models.BaseModel{
			ID: id,
		},
		Phone:   ctx.Query("phone"),
		TypesID: typesId,
	}
	data, total, err := i.FindByPage(utils.PageQuery(ctx))
	if err != nil {
		panic(err.Error())
	}
	response.Success(ctx, response.Body{
		Data: map[string]interface{}{
			"data":  data,
			"total": total,
		},
	})
}

// @Tags         质保管理
// @Summary      质保不同条件分页查询
// @Param        phone	query	string  false  "电话"
// @Param        carNum	query	string  false  "车牌号码"
// @Param        carId	query	string  false  "车架号码"
// @Param        pageSize	query	string  false  "分页大小"
// @Param        pageNum	query	string  false  "分页数量"
// @Success      200       {object}  response.Body
// @Router       /quality/findByPageOr [get]
func FindQualityByPageOr(ctx *gin.Context) {

	i := models.Quality{
		Phone:  ctx.Query("phone"),
		CarNum: ctx.Query("carNum"),
		CarID:  ctx.Query("carId"),
	}
	data, total, err := i.FindByPageOr(utils.PageQuery(ctx))
	if err != nil {
		panic(err.Error())
	}
	response.Success(ctx, response.Body{
		Data: map[string]interface{}{
			"data":  data,
			"total": total,
		},
	})
}

// @Tags         质保管理
// @Summary      质保删除
// @Param        id	query	string  false  "质保id"
// @Success      200       {object}  response.Body
// @Router       /quality/delete [DELETE]
func DeleteQuality(ctx *gin.Context) {
	deleteId, _ := strconv.Atoi(ctx.Query("id"))

	i := models.Quality{
		BaseModel: models.BaseModel{
			ID: deleteId,
		},
	}
	err := i.Delete()
	if err != nil {
		panic(err)
	}
	response.Success(ctx)
}
